package com.ruoyi.web.controller.data;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.CollectionTask;
import com.ruoyi.system.service.ICollectionTaskService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 采集任务信息Controller
 *
 * @author ruoyi
 * @date 2025-04-30
 */
@Controller
@RequestMapping("/system/task")
public class CollectionTaskController extends BaseController
{
    private String prefix = "system/task";

    @Autowired
    private ICollectionTaskService collectionTaskService;

    @RequiresPermissions("system:task:view")
    @GetMapping()
    public String task()
    {
        return prefix + "/task";
    }

    /**
     * 查询采集任务信息列表
     */
    @RequiresPermissions("system:task:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CollectionTask collectionTask)
    {
        startPage();
        List<CollectionTask> list = collectionTaskService.selectCollectionTaskList(collectionTask);
        return getDataTable(list);
    }

    /**
     * 导出采集任务信息列表
     */
    @RequiresPermissions("system:task:export")
    @Log(title = "采集任务信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CollectionTask collectionTask)
    {
        List<CollectionTask> list = collectionTaskService.selectCollectionTaskList(collectionTask);
        ExcelUtil<CollectionTask> util = new ExcelUtil<CollectionTask>(CollectionTask.class);
        return util.exportExcel(list, "采集任务信息数据");
    }

    /**
     * 新增采集任务信息
     */
    @RequiresPermissions("system:task:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存采集任务信息
     */
    @RequiresPermissions("system:task:add")
    @Log(title = "采集任务信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CollectionTask collectionTask)
    {
        return toAjax(collectionTaskService.insertCollectionTask(collectionTask));
    }

    /**
     * 修改采集任务信息
     */
    @RequiresPermissions("system:task:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CollectionTask collectionTask = collectionTaskService.selectCollectionTaskById(id);
        mmap.put("collectionTask", collectionTask);
        return prefix + "/edit";
    }

    /**
     * 修改保存采集任务信息
     */
    @RequiresPermissions("system:task:edit")
    @Log(title = "采集任务信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CollectionTask collectionTask)
    {
        return toAjax(collectionTaskService.updateCollectionTask(collectionTask));
    }

    /**
     * 删除采集任务信息
     */
    @RequiresPermissions("system:task:remove")
    @Log(title = "采集任务信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(collectionTaskService.deleteCollectionTaskByIds(ids));
    }

    /**
     * 执行采集任务
     */
    @RequiresPermissions("system:task:execute")
    @Log(title = "采集任务执行", businessType = BusinessType.OTHER)
    @PostMapping("/execute")
    @ResponseBody
    public AjaxResult execute(Long id)
    {
        try {
            return toAjax(collectionTaskService.executeTask(id));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }
}
